package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.CheckApply;
import his.entity.FmedItem;
import his.service.CheckApplyService;
import his.service.FmedItemService;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/medical_technician/")
public class CheckApplyController {

    @Autowired
    CheckApplyService checkApplyService;

    @Autowired
    FmedItemService fmedItemService;

    @GetMapping("check_patient_query/{keyword}")
    @ResponseBody
    public CommonResponse<List<CheckApply>> get_patient_item(@PathVariable String keyword)
    {
        return  checkApplyService.get_patient_item(keyword);
    }

    @PostMapping("check_reg_pro")
    @ResponseBody
    public CommonResponse check_item(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int medical_id=Integer.parseInt((String) jsonObject.get("medical_id"));
        int regist_id=Integer.parseInt ((String) jsonObject.get("regist_id"));
        int item_id=Integer.parseInt ((String) jsonObject.get("item_id"));
        String name=(String) jsonObject.get("name");
        String objective=(String) jsonObject.get("objective");
        String position=(String) jsonObject.get("position");
        int is_urgent=Integer.parseInt ((String) jsonObject.get("is_urgent"));
        int num=Integer.parseInt ((String) jsonObject.get("num"));
        int doctor_id=Integer.parseInt ((String) jsonObject.get("doctor_id"));
        int record_type=Integer.parseInt ((String) jsonObject.get("record_type"));

        return  checkApplyService.check_item(medical_id,regist_id,item_id,name,objective, position, is_urgent, num, doctor_id, record_type);
    }

    @PutMapping("check_execute_pro")
    @ResponseBody
    public CommonResponse excute_item(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int medical_id=(int) jsonObject.get("medical_id");
        int regist_id=(int) jsonObject.get("regist_id");
        int item_id=(int) jsonObject.get("item_id");
        int checkOperID=Integer.parseInt((String) jsonObject.get("checkOperID"));
        return  checkApplyService.excute_item(medical_id,regist_id,item_id,checkOperID);
    }


    @PutMapping("check_result_entry")
    @ResponseBody
    public CommonResponse insert_item_result(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int medical_id=(int) jsonObject.get("medical_id");
        int regist_id=(int) jsonObject.get("regist_id");
        int item_id=(int) jsonObject.get("item_id");
        int result_oper_id=Integer.parseInt((String) jsonObject.get("result_oper_id"));
        String result=(String) jsonObject.get("result");

        return  checkApplyService.insert_item_result(medical_id,regist_id,item_id,result_oper_id,result);
    }

    @GetMapping("medical_manage/all")
    @ResponseBody
    public CommonResponse<List<FmedItem>> getall_item()
    {
        return  fmedItemService.getall_item();
    }

    @GetMapping("medical_manage/query/{keyword}")
    @ResponseBody
    public CommonResponse<List<FmedItem>> search_item(@PathVariable String keyword)
    {
        return  fmedItemService.search_item(keyword);
    }

    @DeleteMapping("medical_manage/del")
    @ResponseBody
    public CommonResponse<List<FmedItem>> delete_item(@RequestParam String item_code)
    {
        return  fmedItemService.delete_item(item_code);
    }

}
